package com.meizhuo.listview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.meizhuo.listview.DataSaveUtil.ListDataSave;
import com.meizhuo.listview.api.HttpClient;
import com.meizhuo.listview.network.GetRegister_Interface;
import com.meizhuo.listview.network_data.recv_data.Register_body;
import com.meizhuo.listview.network_data.send_data.Register;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class RegisterActivity extends Activity implements View.OnClickListener {

    private Context mContext ;
    private EditText nickname;
    private EditText re_username;
    private EditText re_password;
    private EditText re_affirm;
    private Button re_register;

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.re_register :
                submit();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        initView();
        mContext = getApplicationContext() ;
    }


    private void initView() {

        nickname = (EditText) findViewById(R.id.re_nickname);
        nickname.setOnClickListener(this);
        re_username = (EditText) findViewById(R.id.re_username);
        re_username.setOnClickListener(this);
        re_password = (EditText) findViewById(R.id.re_password);
        re_password.setOnClickListener(this);
        re_affirm = (EditText) findViewById(R.id.re_affirm);
        re_affirm.setOnClickListener(this);
        re_register = (Button) findViewById(R.id.re_register);
        re_register.setOnClickListener(this);
    }

    private void submit() {
        // validate
        String nicknameString = nickname.getText().toString().trim();
        if (TextUtils.isEmpty(nicknameString)) {
            Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show();
            return;
        }

        String username = re_username.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }

        String password = re_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String affirm = re_affirm.getText().toString().trim();
        if (TextUtils.isEmpty(affirm)) {
            Toast.makeText(this, "请确认密码", Toast.LENGTH_SHORT).show();
            return;
        }

        if(!affirm.equals(password)){
            Toast.makeText(this, "输入的两次密码不一样", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        // 在这里通过网络请求将用户名和密码的信息发送给后端，如果验证正确，则跳转到登陆页面，否则提示用户账号和密码错误
        // 创建 网络请求接口 的实例,范型传入返回的结果
        HttpClient<Register_body> httpClient = HttpClient.getInstance();

        // 请求体传入接收的结果
        GetRegister_Interface request = httpClient.getHttpClient().create(GetRegister_Interface.class);

        //对 发送请求 进行封装
        Call<Register_body> call = request.getCall(new Register(username, password,nicknameString));

        httpClient.start(call, new Callback<Register_body>() {
            //请求成功时回调
            @Override
            public void onResponse(Call<Register_body> call, final Response<Register_body> response) {

                if (response.body() == null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(RegisterActivity.this, "请求出错", Toast.LENGTH_SHORT).show();
                        }
                    });
                    return;
                }

                // 等于200 说明请求成功，跳转页面
                if (response.body().getStatus() == 200) {

                    // 登陆成功，同时把注册的数据保存到本地
                    ListDataSave.getInstance(mContext,"Userdata").setData("user",response.body().getData());

                    // 进行跳转
                    Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();

                } else {

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(RegisterActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    });

                }

            }
            //请求失败时候的回调
            @Override
            public void onFailure(Call<Register_body> call, Throwable throwable) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }
}
